<?php
class ringtoneCheckOutAction extends sfActions{
	public function execute(){
		$ringtoneId = $this->getRequestParameter("ringtoneId");
		
		$this->adminId = $this->getUser()->getAttribute('adminId');
		$this->adminLevel = $this->getUser()->getAttribute('adminLevel');
		
		$retVal = new stdClass();
		$retVal->success = false;

		$cr = new Criteria();
		$cr->add(RingtonePeer::ID, $ringtoneId);
		$_ringtoneCheckOut = RingtonePeer::doSelectOne($cr);

		if ($_ringtoneCheckOut)
		{
			$__crAdminCheckout = $_ringtoneCheckOut->getAdminCheckoutId();
			if ($__crAdminCheckout && $__crAdminCheckout != $this->adminId)
			{
				$retVal->success = false;
				$cr = new Criteria();
				$cr->add(AdminPeer::ID, $__crAdminCheckout);
				$__admin = AdminPeer::doSelectOne($cr);
				$retVal->errorMessage = "Ringtone này đang được sửa bởi " . $__admin->getUsername();
			}
			else
			{
				$_ringtoneCheckOut->setAdminCheckoutId($this->adminId);
				$_ringtoneCheckOut->setCheckoutTime("now");
				$_ringtoneCheckOut->save();
				$retVal->success = true;
			}
		}

		echo FastJSON::encode($retVal);
		exit();
	}
}
?>